package com.sky.service.category;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.BaseContext;
import com.sky.dto.category.CategoryPageDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.vo.CategoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;

/**
 * @Author 褚金龙
 * @Date 2024/8/31 21:23
 */

@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService{
   @Autowired
   private CategoryMapper categoryMapper;

    /**
     * 分页查询
     * @param categoryPageDTO
     * @return
     */
    @Override
    public PageResult pageService(@RequestBody CategoryPageDTO categoryPageDTO) {
        PageHelper.startPage(categoryPageDTO.getPage(),categoryPageDTO.getPageSize());
        Page<Category> page = categoryMapper.page(categoryPageDTO);

        return new PageResult(page.getTotal(),page);
    }

    /**
     * 按id查询数据
     * @param id
     * @return
     */
    @Override
    public CategoryVO queryCategoryService(Integer id) {
        CategoryVO categoryVO= categoryMapper.queryCategory(id);
        return categoryVO;
    }

    /**
     * 修改分类
     * @param categoryVO
     * @return
     */
    @Override
    public void updateCategory(CategoryVO categoryVO) {
        // 设置修改时间
        Category category=new Category();


        //把categoryVO拷贝进category
        BeanUtils.copyProperties(categoryVO,category);
        category.setId(categoryVO.getId());

         categoryMapper.updateCategoryMapper(category);

    }

    /**
     * 修改分类启用和禁用
     * @param status
     * @param id
     */
    @Override
    public void statusUpdate(Integer status,Integer id) {
        categoryMapper.updateStatusMapper(status,id);
    }

    /**
     * 删除分类
     * @param id
     */
    @Override
    public void deleteCategoryService(Integer id) {
        categoryMapper.deleteCategoryMapper(id);
    }

    /**
     * 添加分类
     * @param category
     */
    @Override
    public void insertCategoryService(Category category) {
        category.setId(BaseContext.getCurrentId());

        categoryMapper.insertCategoryMapper(category);
    }
}
